/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.agro.formularios;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.marcelo.comun.controller.InterfaceController;
import org.marcelo.comun.efectos.MyGlassPane;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.util.WeakListeners;
import org.openide.windows.WindowManager;

/**
 *
 * @author marcelo
 */
public abstract class AbstractFormulario extends JPanel implements ActionListener, InterfaceController{
    
    
    
    private DialogDescriptor dd;
    private String titulo;
    private Boolean isModal;
    
    private JFrame frame;
    private JComponent glassPane;
    private JComponent oldGlass;
    
    private void initGlass() {
        glassPane = new MyGlassPane();
        frame = (JFrame) WindowManager.getDefault().getMainWindow();
        oldGlass = (JComponent) frame.getGlassPane();
        frame.getRootPane().setGlassPane(glassPane);
        glassPane.setVisible(true);
    }
    
    private void initListeners() {
        
        dd.addPropertyChangeListener(WeakListeners.propertyChange( new  PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(DialogDescriptor.PROP_VALUE)  
                    && evt.getNewValue().equals( DialogDescriptor.CLOSED_OPTION)) {
                    if (doOnExit() )
                        setInvisibleGlass();
                }
            }
        } , dd) );
    }
    
    /**
     * Muestra el formulario asociado a este controller
     */
    @Override
    public void showPanel() {
        init();
        initGlass();
        dd = new DialogDescriptor(this, titulo, isModal, this);
        dd.setClosingOptions(new Object[] {});
        initListeners();
        DialogDisplayer.getDefault().notifyLater(dd);
    }

    public AbstractFormulario(String titulo, Boolean isModal) {
        this.titulo = titulo;
        this.isModal = isModal;
    }
    
    
       

    @Override
    public void actionPerformed(ActionEvent e) {
        
         if (e.getSource() == DialogDescriptor.OK_OPTION ){
             if (doOnCloseOk()) {
                 setInvisibleGlass();
                 dd.setClosingOptions(null);
             }
         }
         else if (e.getSource() == DialogDescriptor.CANCEL_OPTION ) {
             if (doOnCancel()) {
                 setInvisibleGlass();
                 dd.setClosingOptions(null);
             }
         }
        
    }
    
    /**
     * Metodo para implementar la logica al cerrar presionar OK
     * si la logica se cumplio cerrar el dialogo
     * sino seguir mostrando
     */
    public abstract Boolean doOnCloseOk();
    
    /**
     * Metodo para implementar la logica al cerra con Cancel
     * si la logica se cumplio cerrar el dialogo
     * si no se cumplio seguir mostrando el dialogo
     */
    public abstract Boolean doOnCancel();
    
    /**
     * Logica a ser ejecutada al momento de salir
     * @return 
     */
    public abstract Boolean doOnExit();
    
    
    
    protected abstract void init();
    
     private void setInvisibleGlass() {
        SwingUtilities.invokeLater(
            new Runnable() {
                @Override 
                public void run(){
                    glassPane.setVisible(false);
                    frame.setGlassPane(oldGlass);
                }
            }      
        );
     }
    

       
}
